package common

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

// 定义一个 通用的返回结构体
// 参考前端要求的 交互字段 D:\nyy_work\fe-work\big-devops\mock\_util.ts
// export function resultSuccess<T = Recordable>(result: T, { message = 'ok' } = {}) {
//  return {
//    code: ResultEnum.SUCCESS,
//    result,
//    message,
//    type: 'success',
//  };
//}
/*
	{
	    "code": 0,
	    "result": {
	        "roles": [
	            {
	                "roleName": "Super Admin",
	                "value": "super"
	            }
	        ],
	        "userId": "1",
	        "username": "vben",
	        "token": "fakeToken1",
	        "realName": "Vben Admin",
	        "desc": "manager"
	    },
	    "message": "ok",
	    "type": "success"
	}
*/

type FrontLabelOption struct {
	Label    string              `json:"label"`
	Value    string              `json:"value"`
	Children []*FrontLabelOption `json:"children"`
}

type OneLabels struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

type K8sBatchRequestCommon struct {
	Cluster string               `json:"cluster"`
	Items   []K8sBatchRequestOne `json:"items"`
}

type K8sBatchRequestOne struct {
	Namespace string `json:"namespace"`
	Name      string `json:"name"`
}
type K8sModOneObjCommonRequest struct {
	Cluster   string `json:"cluster"`
	Namespace string `json:"namespace"`
	Name      string `json:"name"`
}

type CommonCheckResult struct {
	Success bool   `json:"success"`
	Err     string `json:"err"`
}

type CommonSelectOneItem struct {
	Label string `json:"label"`
	Value string `json:"value"`
}

type FromKvOneItem struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

type CommonSelectOneItemValueInt struct {
	Label string `json:"label"`
	Value int    `json:"value"`
}

type AlertmanagerSilenceResponse struct {
	Status string `json:"status"`
	Data   struct {
		SilenceId string `json:"silenceId"`
	} `json:"data"`
}

type BaseResp struct {
	Code    int         `json:"code"` // 这里的code不是http的状态码 ，而是前后端交互定义的
	Data    interface{} `json:"result"`
	Message string      `json:"message"`
	Type    string      `json:"type"`
}

const (
	ERROR   = 7
	SUCCESS = 0
)

// http的200相应，但是 不代表code是OK
func Result(code int, data interface{}, msg string, c *gin.Context) {
	// 开始时间
	c.JSON(http.StatusOK, BaseResp{
		Code:    code,
		Data:    data,
		Message: msg,
		Type:    "",
	})
}

func Ok(c *gin.Context) {
	Result(SUCCESS, map[string]interface{}{}, "操作成功", c)
}

func OkWithMessage(message string, c *gin.Context) {
	Result(SUCCESS, map[string]interface{}{}, message, c)
}

func OkWithData(data interface{}, c *gin.Context) {
	Result(SUCCESS, data, "查询成功", c)
}

func OkWithDetailed(data interface{}, message string, c *gin.Context) {
	Result(SUCCESS, data, message, c)
}
func Fail(c *gin.Context) {
	Result(ERROR, map[string]interface{}{}, "操作失败", c)
}

func FailWithMessage(message string, c *gin.Context) {
	Result(ERROR, map[string]interface{}{}, message, c)
}

func FailWithDetailed(data interface{}, message string, c *gin.Context) {
	Result(ERROR, data, message, c)
}

// 参数错误
func Result400(code int, data interface{}, msg string, c *gin.Context) {
	// 开始时间
	c.JSON(http.StatusBadRequest, BaseResp{
		Code:    code,
		Data:    data,
		Message: msg,
		Type:    "",
	})
}

func Result403(code int, data interface{}, msg string, c *gin.Context) {
	// 开始时间
	c.JSON(http.StatusForbidden, BaseResp{
		Code:    code,
		Data:    data,
		Message: msg,
		Type:    "",
	})
}

// 没权限
func Result401(code int, data interface{}, msg string, c *gin.Context) {
	// 开始时间
	c.JSON(http.StatusUnauthorized, BaseResp{
		Code:    code,
		Data:    data,
		Message: msg,
		Type:    "",
	})
}

func Result5XX(code int, data interface{}, msg string, c *gin.Context) {
	// 开始时间
	c.JSON(http.StatusInternalServerError, BaseResp{
		Code:    code,
		Data:    data,
		Message: msg,
		Type:    "",
	})
}

func ReqBadFailWithMessage(message string, c *gin.Context) {
	Result400(ERROR, map[string]interface{}{}, message, c)
}

func ReqBadFailWithWithDetailed(data interface{}, message string, c *gin.Context) {
	Result400(ERROR, data, message, c)
}

func Req401WithWithDetailed(data interface{}, message string, c *gin.Context) {
	Result401(ERROR, data, message, c)
}

func Req403WithWithMessage(message string, c *gin.Context) {
	Result403(ERROR, map[string]interface{}{}, message, c)
}

func Req5XXWithWithDetailed(data interface{}, message string, c *gin.Context) {
	Result401(ERROR, data, message, c)
}
